package com.ssy.lingxi.platform.template.service.serviceimpl.adorn;

import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.platform.template.service.entity.adorn.AppAdorn;
import com.ssy.lingxi.platform.template.service.model.dto.adorn.AppAdornDTO;
import com.ssy.lingxi.platform.template.service.model.dto.common.TemplateIdDTO;
import com.ssy.lingxi.platform.template.service.repository.AppAdornRepository;
import com.ssy.lingxi.platform.template.service.service.adorn.AppEnterpriseService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 模板装修 - APP企业商城装修 - 业务实现层
 * @author fhj
 * @version 2.0.0
 * @date 2020/12/09
 */
@Slf4j
@Service
public class AppEnterpriseServiceImpl implements AppEnterpriseService {

    @Resource
    private AppAdornRepository appAdornRepository;

    @Override
    @Transactional
    @ApiOperation(value = "保存APP企业商城装修")
    public Wrapper<Void> save(AppAdornDTO dto, UserLoginCacheDTO user) {
        AppAdorn appAdorn = appAdornRepository.findByTemplateId(dto.getTemplateId());
        appAdorn = appAdorn == null ? new AppAdorn() : appAdorn;
        appAdorn.setTemplateId(dto.getTemplateId());
        appAdorn.setMemberId(user.getMemberId());
        appAdorn.setRoleId(user.getMemberRoleId());
        if(dto.getAdornContent() != null) {
            appAdorn.setAdornContent(dto.getAdornContent());
        }
        if(dto.getCategoryAdornContent() != null) {
            appAdorn.setCategoryAdornContent(dto.getCategoryAdornContent());
        }
        appAdornRepository.saveAndFlush(appAdorn);
        return Wrapper.success();
    }

    @Override
    @ApiOperation(value = "获取APP企业商城装修")
    public Wrapper<AppAdorn> find(TemplateIdDTO dto) {
        return Wrapper.success(appAdornRepository.findByTemplateId(dto.getTemplateId()));
    }
}
